package com.zdx.yswl.ui.tool;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zdx.yswl.ui.bean.Device;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lupengwei on 2015/8/12.
 */

public class SqliteDevice
{
    private SqliteDBHelper dbHelper;
    private SQLiteDatabase db;
    private Context context;
    private List<Device> listDevice = new ArrayList<Device>();

    public SqliteDevice(Context context)
    {
        this.context = context;
        this.dbHelper = new SqliteDBHelper(context, SqliteDBHelper.dbName, null, 1);
        db = dbHelper.getWritableDatabase();
    }

    // 获取数据库中的设备
    public List<Device> getLsitDevFromSql()
    {
        Cursor cursor = db.query(SqliteDBHelper.tableName, null, null, null, null, null, null);
        for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
        {
            String deviceName = cursor.getString(cursor.getColumnIndex("deviceName"));
            String userName = cursor.getString(cursor.getColumnIndex("userName"));
            String pwd = cursor.getString(cursor.getColumnIndex("pwd"));
            String tag = cursor.getString(cursor.getColumnIndex("tag"));
            int bind = cursor.getInt(cursor.getColumnIndex("bind"));
            Log.e("数据库", "deviceName:" + deviceName + "  " +
                    "userName:" + userName + "  " +
                    "pwd:" + pwd + "  " +
                    "bind:" + bind + "  " +
                    "tag:" + tag);
            Device deviceTemp = new Device("10.10.0.1", 9101, deviceName.substring(0, 1),
                    Integer.parseInt(deviceName.substring(1, deviceName.length())), userName, pwd, 1);

            if(bind == 1)
            {
                deviceTemp.setBind(true);
            }
            else
            {
                deviceTemp.setBind(false);
            }

            deviceTemp.setTag(tag);

            listDevice.add(deviceTemp);
        }
        cursor.close();

        return listDevice;
    }

    // 添加设备到数据库, bind表示是否绑定成功
    public void addDevToSql(String deviceName, int bind, String tag)
    {
        Boolean isSame = false;
        Cursor cursor = db.query(SqliteDBHelper.tableName, null, null, null, null, null, null);
        for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
        {
            String deviceNameTemp = cursor.getString(cursor.getColumnIndex("deviceName"));
            if(deviceName.equals(deviceNameTemp))
            {
                isSame = true;
                ContentValues values = new ContentValues();
                values.put("bind", bind);
                values.put("tag", tag);
                db.update(SqliteDBHelper.tableName,
                          values,
                          "deviceName=?",
                          new String[]{deviceName});
                db.close();
                db = dbHelper.getWritableDatabase();
                Log.e("数据库", "数据更新成功");
            }
        }
        if(isSame == false)
        {
            ContentValues values = new ContentValues();
            values.put("deviceName", deviceName);
            values.put("userName", "admin");
            values.put("pwd", "123");
            values.put("bind", bind);
            values.put("tag", tag);
            db.insert(SqliteDBHelper.tableName, null, values);
            db.close();
            db = dbHelper.getWritableDatabase();
            Log.e("数据库", "数据添加成功");
        }
        cursor.close();
    }

    // 从数据库中删除设备
    public void delDevFromSql(String devicename)
    {
        db.delete(SqliteDBHelper.tableName, "deviceName=?", new String[]{devicename});
        db.close();
        db = dbHelper.getWritableDatabase();
        Log.e("数据库", "数据删除成功");
    }

    // 遍历删除数据库中所有的设备，如果设备较多，请采取直接删除表的方法删除
    public void delAllDevFromSql()
    {
        ArrayList<Device> listDevlistTemp = new ArrayList<Device>();
        Cursor cursor = db.query(SqliteDBHelper.tableName, null, null, null, null, null, null);
        for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
        {
            String deviceName = cursor.getString(cursor.getColumnIndex("deviceName"));
            String userName = cursor.getString(cursor.getColumnIndex("userName"));
            String pwd = cursor.getString(cursor.getColumnIndex("pwd"));

            Device deviceTemp = new Device("10.10.0.1", 9101, deviceName.substring(0, 1),
                    Integer.parseInt(deviceName.substring(1, deviceName.length())), userName, pwd, 1);
            listDevlistTemp.add(deviceTemp);
        }
        cursor.close();

        if(listDevlistTemp.size() != 0)
        {
            for(int i = 0; i < listDevlistTemp.size(); i++)
            {
                db.delete(SqliteDBHelper.tableName, "deviceName=?", new String[]{listDevlistTemp.get(i).getFullNo()});
            }
            db.close();
            db = dbHelper.getWritableDatabase();
        }
        Log.e("数据库", "删除了本地的设备");
    }

    public void deleteDatabase()
    {
        dbHelper.deleteDatabase(context);
    }

    // 获取用户名，用户名只保存一行
    public String getUserNameFromSql()
    {
        Cursor cursor = db.query(SqliteDBHelper.tableNameUser, null, null, null, null, null, null);
        String userName = null;

        Log.e("cursor", "" + cursor.getCount());
        // 如果数据库中的数据为0
        if(cursor.getCount() > 0)
        {
            // db.execSQL("insert into " + SqliteDBHelper.tableNameUser + "(userName) values('userName')");
            cursor.moveToFirst();
            userName = cursor.getString(cursor.getColumnIndex("userName"));
        }
        cursor.close();

        return userName;
    }

    // 更新用户名
    public void updataUserNameToSql(String userName)
    {
        ContentValues values = new ContentValues();
        values.put("userName", userName);
        db.update(SqliteDBHelper.tableNameUser, values, "userName=?", new String[]{userName});
        db.close();
        db = dbHelper.getWritableDatabase();
        Log.e("数据库", "用户名更新成功");
    }

    // 添加用户
    public void addUserNameToSql(String userName)
    {
        ContentValues values = new ContentValues();
        values.put("userName", userName);
        db.insert(SqliteDBHelper.tableNameUser, null, values);
        db.close();
        db = dbHelper.getWritableDatabase();
        Log.e("数据库", "用户名添加成功");
    }
    
    public void delUserFromSql(String userName)
    {
        db.delete(SqliteDBHelper.tableNameUser, "userName=?", new String[]{userName});
        db.close();
        db = dbHelper.getWritableDatabase();
        Log.e("数据库", "用户名删除成功");
    }
}
